﻿var ue = null;
var vmNoticeIndex = new Vue({
    el: "#divNoticeIndex",
    data: {
        dialogVisible: false,
        formLabelWidth: 80,
        search: {
            title: '',
            uName: '',
            time: '',
            type: ''
        },
        typeList: [
            { id: 1, name: '招考信息' },
            { id: 2, name: '招考要闻' },
            { id: 3, name: '教育动态' },
            { id: 4, name: '系统公告' }
        ],
        ///列表
        pagination: {
            currentpage: 1,
            pagesize: 10,
            total: 0
        },
        loading: false,
        newsList: [],
        dialogTitle: '新增系统公告',
        form: {
            id: 0,
            noticeType: 1,
            title: '',
            content: ''
        },
        rules: {
            title: [
                { required: true, message: '请输入标题', trigger: 'blur' }
            ] 
        }
    },
    mounted: function () {
        UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
        UE.Editor.prototype.getActionUrl = function (action) {
            if (action == "config") {
                return '/ueditor/getconfig';
            }
            else {
                return '/ueditor/upload?action=' + action;
            }
        }
        VmTop.navOn = 5;
        //加载列表数据
        this.getNewslist();
    },
    methods: {
        formatDate: function (row, col, value) {
            return value.split(" ")[0];
        },
        ///返回首页
        initPageIndex: function () {
            this.isInitPage = true;
        },
        ///跳转页面时
        pageChange: function (page) {
            this.pagination.currentpage = page;
            //加载列表数据
            this.getNewslist();
        },
        //查询公告活动动态
        getNewslist: function () {
            var that = this;
            var params = {
                pageIndex: this.pagination.currentpage,
                pageSize: this.pagination.pagesize,
                noticeType: this.search.type,
                uName: this.search.uName,
                title: this.search.title
            };
            if (that.isInitPage) {
                that.pagination.currentpage = 1;
                that.isInitPage = false;
            }
            that.loading = true;
            $.getJSON("/Notice/GetNoticeListByPage", params, function (result) {
                that.newsList = result.list;
                that.pagination.total = result.total;
                that.loading = false;
            });
        },
        handleClose: function () {
        },
        //重置form验证
        resetForm: function (formName) {
            if (this.$refs[formName] !== undefined) {
                this.$refs[formName].resetFields();
            }
        },
        //清除form验证
        clearFormValidate: function (formName) {
            if (this.$refs[formName] !== undefined) {
                this.$refs[formName].clearValidate();
            }
        },
        udeitorOpen: function () {
            this.$nextTick(function () {
                ue = UE.getEditor('Content');
                ue.addListener('wordcountoverflow', function () {
                    //alert("字数已经超过最大允许值，系统将自动删除超过的字符", { icon: 7 });
                    var chars = ue.getContentTxt();
                    var schars = chars.substring(0, 10000);
                    ue.setContent(schars);
                });
            });
        },
        closeDialog: function (done) {
            this.form = {
                id: 0,
                noticeType: 1,
                title: '',
                content: ''
            };
            var that = this;
            setTimeout(function () {
                that.ueditor.setContent("");
            }, 500);
            done();
        },
        //新增
        addNews: function () {
            this.resetForm('form');
            this.form = {
                id: 0,
                noticeType: 1,
                title: '',
                content: ''
            };
            this.dialogTitle = '添加信息';
            var that = this;
            setTimeout(function () {
                ue.setContent("");
            }, 500);
            this.dialogVisible = true;
        },
        //修改
        updateNews: function (row) {
            var that = this;
            this.resetForm('form');
            this.isEdit = true;
            this.form = {
                id: row.id,
                noticeType: row.noticeType,
                title: row.title,
                content: row.content
            };
            this.dialogTitle = '修改信息';
            setTimeout(function () {
                ue.setContent(row.content);
            }, 500);
            this.dialogVisible = true;
        },
        saveNews: function () {
            var that = this;
            this.$refs.form.validate(function (valid) {
                if (valid) {
                    that.form.content = ue.getContent();
                    $.post("/Notice/SaveNotice", that.form, function (result) {
                        if (result.suc) {
                            //加载列表数据
                            that.getNewslist();
                            that.dialogVisible = false;
                        }
                        VmYz.message(result.msg, result.suc);
                        that.loading = false;
                    });
                    that.loading = true;

                } else {
                    VmYz.message('请填写正确信息！', 'warning');
                    return false;
                }
            });
        },
        //删除
        delNews: function (id) {
            that = this;
            VmYz.confirm("您确定要删除吗?", function () {
                $.post("/Notice/DelNotice", { id: id },
                    function (result) {
                        if (result.suc) {
                            //加载列表数据
                            that.getNewslist();
                        }
                        VmYz.message(result.msg, result.suc);
                    });
            })
        },
        //查看详情
        intoDetail: function (id) {
            location.href = "/Notice/Detail/" + id;
        }
    }
})